$(function () {
    var form = window.form_query;
    var now = moment().format("YYYY-MM-DD");
    var timeLimitStart = form.timeLimitStart.value;
    var timeLimitEnd = form.timeLimitEnd.value;
    var startDate = moment(now).subtract(30, 'days').startOf('day'), endDate = moment(now).endOf('day');
    if (timeLimitStart && timeLimitEnd) {
        startDate = moment(parseInt(timeLimitStart));
        endDate = moment(parseInt(timeLimitEnd));
    }
    
    if(!timeLimitStart){
    	form.timeLimitStart.value = startDate.startOf('day').format("x");
    }
    if(!timeLimitEnd){
    	form.timeLimitEnd.value = endDate.startOf('day').format("x");
    }
    
    

    $("#timelimitstart_and_timelimitstart").daterangepicker({
        "startDate": startDate,
        "endDate": endDate,
        "opens": "center",
        "autoApply": true,
        "linkedCalendars": false,
        "ranges": {
            "今天": [moment(now).startOf('day'), moment(now).endOf('day')],
            "昨天": [moment(now).subtract(1, 'days').startOf('day'), moment(now).subtract(1, 'days').endOf('day')],
            "最近7天": [moment(now).subtract(7, 'days').startOf('day'), moment(now).endOf('day')],
            "最近30天": [moment(now).subtract(30, 'days').startOf('day'), moment(now).endOf('day')],
            "本月": [moment(now).startOf('month'), moment(now).endOf('month')],
            "上个月": [moment(now).subtract(1, 'months').startOf('month'), moment(now).subtract(1, 'months').endOf('month')]
        },
        "locale": {
            "firstDay": 1,
            "format": "YYYY-MM-DD",
            "separator": " 至 ",
            "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
            "monthNames": ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
            customRangeLabel: "自定义范围"
        }
    }, function (start, end, label) {
        form.timeLimitStart.value = start.startOf('day').format("x");
        form.timeLimitEnd.value = end.endOf('day').format("x");
    });


    var chart = echarts.init(document.getElementById('main'));
    var chartOption = {
        title: {
            text: '商品扫码量统计'
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {},
        xAxis: {
            type: "time",
            name: "日期",
            interval: 86400000
        },
        yAxis: {
            type: "value",
            name: "扫码量"
        }
    };
    chart.setOption(chartOption);


    $("[name='btn_search']").click(function () {
        var keyword = form.keyword.value;
        var timeLimitStart = form.timeLimitStart.value;
        var timeLimitEnd = form.timeLimitEnd.value;

        chart.showLoading();
        $.get("/stastics/proscan", {keyword: keyword, timeLimitStart: timeLimitStart, timeLimitEnd: timeLimitEnd}, function (re) {

            chart.hideLoading();
            var data = re.data;
            var series = [];
            var legendData = [];
            for (var i in data) {
                var points = data[i];
                var series_data = [];
                for (var j in points) {
                    var point = points[j];
                    series_data.push({
                        value: [moment(point.scanDate).format("YYYY/MM/DD"), point.scanCount]
                    });
                   
                }
                var name = points[0].productName;
                legendData.push(name);
                series.push({name: name, type: 'line', data: series_data});
            }

            chartOption.title.subtext = keyword ? "(筛选条件:" + keyword + ")" : "";
            chartOption.series = series;
            chartOption.legend.data = legendData;
            chart.setOption(chartOption, true);
        });
    });

    $("[name='btn_search']").click();


});